package cn.lingyangwl.agile.system.controller;

import cn.lingyangwl.agile.model.constants.CommonCons;
import cn.lingyangwl.agile.ram.ActionType;
import cn.lingyangwl.agile.ram.RamResource;
import cn.lingyangwl.agile.system.model.rqrs.tenant.*;
import cn.lingyangwl.agile.system.service.SysTenantPackageService;
import cn.lingyangwl.framework.core.response.Resp;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * @author shenguangyang
 */
@Tag(name = "租户套餐")
@RestController
@RequestMapping("/tenantPackage")
public class SysTenantPackageController {
    @Resource
    private SysTenantPackageService sysTenantPackageService;

    @PostMapping("/save")
    @Operation(summary = "创建租户套餐")
    @RamResource(action = "sys:tenantPackage:create", type = ActionType.WRITE, name = "创建租户套餐")
    public Resp<Long> saveTenantPackage(@Valid @RequestBody TenantPackageSaveReq req) {
        return Resp.ok(sysTenantPackageService.saveTenantPackage(req));
    }

    @PostMapping("/update")
    @Operation(summary = "更新租户套餐")
    @RamResource(action = "sys:tenantPackage:update", type = ActionType.WRITE, name = "更新租户套餐")
    public Resp<Boolean> updateTenantPackage(@Valid @RequestBody TenantPackageUpdateReq req) {
        sysTenantPackageService.updateTenantPackage(req);
        return Resp.ok(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除租户套餐")
    @Parameter(name = "id", description = "编号", required = true)
    @RamResource(action = "sys:tenantPackage:delete", type = ActionType.WRITE, name = "删除租户套餐")
    public Resp<Boolean> deleteTenantPackage(@RequestParam("id") Long id) {
        sysTenantPackageService.deleteTenantPackage(id);
        return Resp.ok(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得租户套餐")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @RamResource(action = "sys:tenantPackage:query", type = ActionType.READ, name = "查询租户套餐")
    public Resp<TenantPackageResp> getTenantPackage(@RequestParam("id") Long id) {
        TenantPackageResp tenantPackage = sysTenantPackageService.getTenantPackage(id);
        return Resp.ok(tenantPackage);
    }

    @GetMapping("/page")
    @Operation(summary = "获得租户套餐分页")
    @RamResource(action = "sys:tenantPackage:query", type = ActionType.READ, name = "查询租户套餐")
    public Resp<IPage<TenantPackageResp>> getTenantPackagePage(@Valid TenantPackagePageReq req) {
        IPage<TenantPackageResp> page = sysTenantPackageService.pageTenantPackage(req);
        return Resp.ok(page);
    }

    @GetMapping("/list")
    @RamResource(action = "sys:tenantPackage:query", type = ActionType.READ, name = "查询租户套餐")
    @Operation(summary = "获取租户套餐精简信息列表", description = "只包含被开启的租户套餐，主要用于前端的下拉选项")
    public Resp<List<TenantPackageResp>> listTenantPackage(@Validated TenantPackageListReq req) {
        // 获得角色列表，只要开启状态的
        req.setStatus(CommonCons.UseStats.NORMAL);
        List<TenantPackageResp> list = sysTenantPackageService.listTenantPackage(req);
        return Resp.ok(list);
    }


}
